import React, { Component } from 'react';
import './index.css'
class Item extends Component {
  state = {mouse:false}

  //鼠标移入移出的回调
  handleMouse = (flag)=>{
    return ()=>{
      this.setState({mouse:flag})
    }
  }

  //点击删除按钮
  handleDelete = (id)=>{
    //console.log('通知APP删除',id)
    if(window.confirm('确定删除吗？')){
      this.props.deleteTodo(id)
    }
  }

  handleCheck = (id)=>{
    return (event)=>{
      //console.log(id,event.target.checked);
      this.props.updateTodo(id,event.target.checked)
    }
  }
  render() {
    const {id,name,done} = this.props
    const {mouse} = this.state
    return (
      <li style={{backgroundColor:mouse?'#ddd':'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)} >
        <label>
          <input type="checkbox" checked={done} onChange={this.handleCheck(id)}/>
          <span>{name}</span>
        </label>
        <button onClick={()=>this.handleDelete(id)} className="btn btn-danger" style={{display:mouse?"block":"none"}}>删除</button>
      </li>
    );
  }
}

export default Item;